### This script will intersect the microclimate/AWAP dataset with solar data
### Solar data only currently available for the 15th of each month
### This dataset will be a subset of the whole, using only dates from the 15th of the month

# Establishing in, out, and working directories

in.dir = '/home1/99/jc152199/Microclimate Statistical Downscale/To Analyse/'
out.dir ='/home1/99/jc152199/Microclimate Statistical Downscale/To Analyse/'
setwd(in.dir)

# Read in AWAP/micro data and solar data

t.data = read.csv("AWAP_Micro_Topo.csv",header=T)
solar.data = read.csv("solardata.csv", header=T)

# Read in loc data

in.dir = '/home1/99/jc152199/Microclimate Statistical Downscale/Location Data/'
setwd(in.dir)
locs = read.csv('Locs.csv',header=T)

#Combine solar data with locs

names(solar.data)[1]='east'
names(solar.data)[2]='north'
solar.2 = merge(solar.data, locs, by=c("east","north"), all.x=T) # Append site to solar data
solar.data = solar.2[,1:15]   # Remove un-necessary columns after merge
solar.data = solar.data[-which(is.na(solar.data$rad015.asc)==T),] # Remove site with 'NA' for solar data

#  Assign a year column to t.data, assign a Julian column to t.data, make a list of unique years from t.data, and re-format t.data$date to a class 'Date'

t.data$Julian = NA
ttt.data = NA
t.data$year = substr(t.data$date,0,4)
years = unique(t.data$year)
t.data$date = as.Date(paste(t.data$date,sep=""),"%Y-%m-%d")

# Begin a loop that will assign a Julian day to each date based on an Origin Day which is the 1st day of each year

for (yearx in years) {

cat(yearx,'...')
start.day = as.Date(paste(yearx,"01-01",sep="-"),"%Y-%m-%d") # Define start date as January 1st
tt.data = t.data[which(t.data$year==yearx),] # Subset data by year of interest
tt.data$Julian = julian(tt.data$date,origin = start.day) # Assign Julian dates and convert to numerals
ttt.data = rbind(tt.data, ttt.data) # Row bind each year of data to a blank data frame

}

t.data = ttt.data
t.data$solar = NA
rm(ttt.data)

# Re-class both 'site' fields to 'character'

solar.data$site = as.character(solar.data$site)
t.data$site = as.character(t.data$site)

# Use the 'grep' command to subset the dataset, keeping only rows where the date is the 15th and removing site '32141'

t2.data = t.data[grep('-15',t.data$date),]
t2.data = t2.data[-which(t2.data$site=='32141'),]

# Create a list of column names for solar.data, this list must be in chronological order
# If not ordered properly, the for loop afterwards will fall down attempting to match the solar data

tcolnames = c("rad015.asc","rad046.asc","rad075.asc","rad106.asc","rad136.asc","rad167.asc","rad197.asc","rad228.asc","rad259.asc","rad289.asc","rad320.asc","rad350.asc")

#Create a for loop that will examine each row of t2.data individually and replace the 'NA' with the solar value from solar.data.  ii=c(0:nrow(t2.data))

for (ii in 1:nrow(t2.data)) {
  cat(ii,'.../n')
  row.in.solar = which(solar.data$site==t2.data$site[ii]) # This command identifies the row of the solar data that we're interested in
  col.from.solar.data = tcolnames[as.numeric(format(as.Date(t2.data$date[ii]),'%m'))] # This command identifies the column of solar data we're interested in
  t2.data$solar[ii] = solar.data[row.in.solar,col.from.solar.data ] # This command replaces the 'NA' value from t2.data with the solar value from solar.data
  
}

# Write out data

write.csv(x=t2.data, file=paste(out.dir,"solar_regress_all_days.csv",sep=""), row.names=F)




